/**
 *   moviejukebox2osdmod
 *
 *   Copyright (C) 2009  Svetoslav Batchovski
 *
 *   This file is part of moviejukebox2osdmod
 *
 *   moviejukebox2osdmod is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package info.sharo;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;

/**
 * @author Svetoslav Batchovski
 * @mailto: sharan4o@gmail.com
 * 
 */
public class Moviejukebox2osdMod {
	private static Options options;

	/**
	 * Main
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		setOptions();

		CommandLine omCmd = null;
		HelpFormatter omFormater = new HelpFormatter();
		CommandLineParser omParser = new BasicParser();

		try {
			omCmd = omParser.parse(options, args);

			if (omCmd.hasOption('h')) {
				omFormater.printHelp("moviejukebox2osdmod", options);
				return;
			}

		} catch (Exception e) {
			omFormater.printHelp("moviejukebox2osdmod", options);
			System.out.println("Error: " + e.getMessage());
			System.out.println("Example:");
			System.out.println("java -jar moviejukebox2myosd -i /tmp/Jukebox");
			return;
		}

		File xmlDir = new File(omCmd.getOptionValue("i"));

		if (!xmlDir.isDirectory()) {
			omFormater.printHelp("moviejukebox2osdmod", options);
			System.out.println("Error: input parameter is not a directory.");
			System.out.println("Example:");
			System.out.println("java -jar moviejukebox2myosd -i /tmp/Jukebox");
			// help();
			return;
		}

		for (File file : xmlDir.listFiles()) {
			if (file.getName().contains("Other")
					&& file.getName().endsWith("xml")) {
				MovieInfoParser.parseMovieInfoXml(file);
			}
		}

		// create data.txt files
		if (omCmd.hasOption("t"))
			DataFileExporter.exportDataTxtFiles(MovieInfoParser.movieInfoMap,
					xmlDir);
		// create data.xml files
		if (omCmd.hasOption("x"))
			DataFileExporter.exportDataXmlFiles(MovieInfoParser.movieInfoMap,
					xmlDir);
		System.out.println("Done!");
	}

	/**
	 * Print help
	 */
	private static void help() {
		System.out.println("");
		System.out.println("Usage:");
		System.out.println();
		System.out.println("Generates data.txt-files for your movies library.");
		System.out.println();
		System.out.println("Moviejukebox2myosd <directory with xml files>");
		System.out.println();
	}

	/**
	 * Set possible Options
	 */
	private static void setOptions() {

		options = new Options();

		Option help = new Option("h", "help", false, "shows this help.");

		options.addOption(help);

		options
				.addOption(OptionBuilder
						.withLongOpt("input")
						.withDescription(
								"input folder. The "
										+ '"'
										+ "Jukebox"
										+ '"'
										+ " folder with the xml-files generated with Moviejukebox. ")
						.isRequired().hasArg().create("i"));

		options.addOption(OptionBuilder.withLongOpt("xml").withDescription(
				"Generate data.xml files.").create("x"));

		options.addOption(OptionBuilder.withLongOpt("txt").withDescription(
				"Generate data.xml files.").create("t"));

	}
}
